= form_for @user, :url => @user.new_record? ? users_path : user_path(@user), :remote => true do |f|
  = f.error_messages
  .fieldSet
    .fieldSet__title
      Enter user details
    .fieldSet__field
      = f.label :first_name, :class => 'fieldSet__label'
      .fieldSet__input= f.text_field :first_name, :class => 'input input--text', :autofocus => true
    .fieldSet__field
      = f.label :last_name, :class => 'fieldSet__label'
      .fieldSet__input= f.text_field :last_name, :class => 'input input--text'
    .fieldSet__field
      = f.label :email_address, :class => 'fieldSet__label'
      .fieldSet__input
        = f.text_field :email_address, :class => 'input input--text', autocomplete: 'one-time-code'
        - if Postal::Config.oidc.enabled?
          %p.fieldSet__text
            This e-mail address should match the address provided by your OpenID Connect identity provider.

    
    - if Postal::Config.oidc.local_authentication_enabled? && !@user.persisted?
      .fieldSet__field
        = f.label :password, :class => 'fieldSet__label'
        .fieldSet__input
          = f.password_field :password, :class => 'input input--text', :placeholder => '•••••••••••', autocomplete: 'one-time-code'
          - if Postal::Config.oidc.enabled?
            %p.fieldSet__text
              You have enabled OIDC which means a password is not required. If you do not provide
              a password this user will be matched to an OIDC identity based on the e-mail address
              provided above. You may, however, enter a password and this user will be permitted to
              use that password until they have successfully logged in with OIDC.
        
      .fieldSet__field
        = f.label :password_confirmation, "Confirm".html_safe, :class => 'fieldSet__label'
        .fieldSet__input= f.password_field :password_confirmation, :class => 'input input--text', :placeholder => '•••••••••••', autocomplete: 'one-time-code'

  %fieldset.fieldSet
    .fieldSet__title
      What level of access do you wish to grant?
    .fieldSet__titleSubText
      Admin users have full access to all organizations and settings. Non-admin users will only
      have access to the organizations that you select here.
    .fieldSet__field
      .fieldSet__label Admin?
      .fieldSet__input
        = hidden_field_tag 'user[organization_ids][]'
        = f.select :admin, [["Yes - grant admin access", true], ["No - do not grant admin access", false]], {},:class => 'input input--select fieldSet__checkboxListAfter js-checkbox-list-toggle'
        %ul.checkboxList{:class => [@user.admin? ? 'is-hidden' : '']}
          - for org in Organization.order(:name).to_a
            %li.checkboxList__item
              .checkboxList__checkbox= check_box_tag "user[organization_ids][]", org.id, @user.organizations.include?(org), :id => "org_#{org.id}"
              .checkboxList__label
                = label_tag "org_#{org.id}", org.name, :class => 'checkboxList__actualLabel'

  .fieldSetSubmit.buttonSet
    = submit_tag @user.new_record? ? "Add User" : "Save User", :class => 'button button--positive js-form-submit'
    .fieldSetSubmit__delete
      = link_to "Back to user list", :users, :class => 'button button--neutral'
